package com.zx.kitchen.index.common.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.zx.kitchen.index.R;
import com.zx.kitchen.index.common.adapter.BusinessReportAdapter;
import com.zx.kitchen.index.common.entity.BusinessReportDetailBean;
import com.zx.kitchen.index.common.entity.BusinessReportTotalBean;
import com.zx.kitchen.index.common.mvp.presenter.BusinessReportPresenter;
import com.zx.kitchen.index.common.mvp.presenter.PurchaseReportPresenter;
import com.zx.kitchen.index.common.mvp.view.BusinessReportView;
import com.zx.kitchen.index.common.utils.TimeUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/13.
 */

public class BusinessReportActivity extends Activity implements View.OnClickListener, BusinessReportView {
    private ImageView toolbarLeft;
    private TextView toolbarTitle;

    private TextView tabBusiness;
    private TextView tabPurchase;
    private Button btnChart;
    private ListView ListBusinessReport;
    private BusinessReportAdapter mAdapter;
    private List<BusinessReportDetailBean> mList = new ArrayList<>();
    private boolean isPurchase = false;

    private EditText etStartTime;
    private EditText etEndTime;
    private Spinner spDate;
    private String reportType = "d";
    private String startTime = TimeUtil.getToday();
    private String endTime = TimeUtil.getToday();

    private BusinessReportPresenter businessReportPresenter;
    private PurchaseReportPresenter purchaseReportPresenter;
    private ArrayAdapter<String> mArrayAdapter;
    private static String[] mFontArrayString = new String[]{};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_business_report);
        initView();
    }

    private void initView() {
        View view = findViewById(R.id.titleBar);
        toolbarLeft = (ImageView) view.findViewById(R.id.toolbar_leftLin);
        toolbarTitle = (TextView) view.findViewById(R.id.toolbar_title);
        tabBusiness = (TextView) findViewById(R.id.tab_business);
        tabPurchase = (TextView) findViewById(R.id.tab_purchase);
        btnChart = (Button) findViewById(R.id.btn_chart);
        ListBusinessReport = (ListView) findViewById(R.id.list_business_report);
        toolbarTitle.setTextColor(getResources().getColor(R.color.blue));
        toolbarTitle.setText("财务报表");
        tabBusiness.setBackground(getResources().getDrawable(R.drawable.tab_bg_blue));

        View headerView1 = LayoutInflater.from(this).inflate(R.layout.header_select_date, null);
        View headerView2 = LayoutInflater.from(this).inflate(R.layout.header_spinner_date, null);
        ListBusinessReport.addHeaderView(headerView2);
        ListBusinessReport.addHeaderView(headerView1);
        etStartTime = (EditText) headerView1.findViewById(R.id.et_start_time);
        etEndTime = (EditText) headerView1.findViewById(R.id.et_end_time);
        spDate = (Spinner) headerView2.findViewById(R.id.sp_date);

        etStartTime.setText(TimeUtil.getToday());
        etEndTime.setText(TimeUtil.getToday());

        mFontArrayString = new String[]{"日", "周", "月"};
        //        seeting_spinner改变了spinner的默认样式
        mArrayAdapter = new ArrayAdapter<String>(this, R.layout.edit_spinner, mFontArrayString) {
            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
//                    设置spinner展开的Item布局
                    convertView = getLayoutInflater().inflate(R.layout.edit_spinner_item, parent, false);
                }
                TextView spinnerText = (TextView) convertView.findViewById(R.id.spinner_textView);
                spinnerText.setText(getItem(position));
                return convertView;
            }
        };
        spDate.setAdapter(mArrayAdapter);

        businessReportPresenter = new BusinessReportPresenter(getBaseContext(), this);
        purchaseReportPresenter = new PurchaseReportPresenter(getBaseContext(), this);
        mAdapter = new BusinessReportAdapter(getBaseContext(), mList);
        ListBusinessReport.setAdapter(mAdapter);

        setListener();
    }

    private void setListener() {
        toolbarLeft.setOnClickListener(this);
        tabBusiness.setOnClickListener(this);
        tabPurchase.setOnClickListener(this);
        toolbarTitle.setOnClickListener(this);
        btnChart.setOnClickListener(this);
        spDate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        reportType = "d";//d
                        if (isPurchase) {
                            purchaseReportPresenter.getDataFromNet(reportType, startTime, endTime);
                        } else {
                            businessReportPresenter.getDataFromNet(reportType, startTime, endTime);
                        }

                        break;
                    case 1:
                        reportType = "w";//w
                        if (isPurchase) {
                            purchaseReportPresenter.getDataFromNet(reportType, startTime, endTime);
                        } else {
                            businessReportPresenter.getDataFromNet(reportType, startTime, endTime);
                        }
                        break;
                    case 2:
                        reportType = "m";//y
                        if (isPurchase) {
                            purchaseReportPresenter.getDataFromNet(reportType, startTime, endTime);
                        } else {
                            businessReportPresenter.getDataFromNet(reportType, startTime, endTime);
                        }
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        ListBusinessReport.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(BusinessReportActivity.this, BusinessReportDetailActivity.class).
                        putExtra("day", mList.get(position - 2).getD())
                        .putExtra("type", isPurchase));
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_leftLin:
                finish();
                break;
            case R.id.tab_business:
                isPurchase = false;
                toolbarTitle.setText("营业中心信息");
                tabBusiness.setBackground(getResources().getDrawable(R.drawable.tab_bg_blue));
                tabPurchase.setBackgroundColor(getResources().getColor(R.color.white));
                businessReportPresenter.getDataFromNet(reportType, startTime, endTime);
                break;
            case R.id.tab_purchase:
                isPurchase = true;
                toolbarTitle.setText("采购中心信息");
                tabPurchase.setBackground(getResources().getDrawable(R.drawable.tab_bg_blue));
                tabBusiness.setBackgroundColor(getResources().getColor(R.color.white));
                purchaseReportPresenter.getDataFromNet(reportType, startTime, endTime);
                break;
            case R.id.toolbar_title:
                startActivity(new Intent(this, IndexActivity.class));
                break;
            case R.id.btn_chart:
                startActivity(new Intent(this, ChartReportActivity.class)
                        .putExtra("reportType", reportType)
                        .putExtra("isPurchase", isPurchase));
                break;
        }
    }

    @Override
    public void onSuccess(BusinessReportTotalBean businessReportTotalBean) {
        mList.clear();
        mList.addAll(businessReportTotalBean.getDetail());
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFail(String str) {

    }
}
